package com.bocai.kelalacar.pay.simcpux;

import android.content.Context;
import android.widget.Toast;

import com.bocai.kelalacar.bean.WeChatPayBean;
import com.google.gson.Gson;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;


public class WxPayApi {

    private Context mContext;

    private WeChatPayBean mWxPayBean;

    private IWXAPI api;

    private String res;

    public WxPayApi(Context context) {
        this.mContext = context;
    }

    /**
     * 调起微信支付
     */
    public void sendPayReq() {
        mWxPayBean = new Gson().fromJson(res,WeChatPayBean.class);
        api = WXAPIFactory.createWXAPI(mContext, "");
        PayReq req = new PayReq();

        req.appId			= mWxPayBean.getAppid();
        req.partnerId		= mWxPayBean.getPartnerid();
        req.prepayId		= mWxPayBean.getPrepayid();
        req.nonceStr		= mWxPayBean.getNoncestr();
        req.timeStamp		= mWxPayBean.getTimestamp();
        req.packageValue	= mWxPayBean.getPackageX();
        req.sign			= mWxPayBean.getSign();
//        req.extData			= "app data"; // optional
        Toast.makeText(mContext, "正常调起支付", Toast.LENGTH_SHORT).show();
        // 在支付之前，如果应用没有注册到微信，应该先调用IWXMsg.registerApp将应用注册到微信
        api.sendReq(req);
    }

    /**
     * 发起
     */
    public void pay(String res) {
        this.res = res;
        sendPayReq();
    }

}